<?php


namespace  app\man\controller;

use think\Controller;
use think\Db;
use think\Cache;
use think\Request;
class Config extends Common
{
	public function  index()
	{
		$conf=Db::name('sys_config')->where('id',1)->find();
		$route=APP_PATH.'route.php';
		$conf['code']=file_get_contents($route);
		$this->assign('conf',$conf);
		return $this->fetch('index');
	}

	public function uploadFile(){
		$file = request()->file('filetxt');
	
	    $info = $file->validate(['size'=>15678,'ext'=>'txt'])->move(ROOT_PATH . 'public','');
        if($info){
             $res['code']=122;
        }else{
            // 上传失败获取错误信息
             $res['code']=$file->getError();
        }
	    return json($res);
	}

	public function listFile(){
		$files = array();
		$path_handler = opendir(ROOT_PATH.'public/');
		while($file = readdir($path_handler)){
			if($file!='.'||$file!='..'){
				if(is_file($file)&&$this->get_extension($file)=='txt'){
					array_push($files,$file);
				}
			}
		}

		$this->assign('list',$files);
		return $this->fetch('list');
	}
	private function get_extension($file){
        return pathinfo($file, PATHINFO_EXTENSION);
    }

    public function deleteTxt(){
    	$filename = input('id');
    	$path = ROOT_PATH.'public/'.$filename.'.txt';
    	if(!file_exists($path)){
    		$this->error('这个文件不存在');
    	}
    	if(unlink($path)){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}
    }
	

	public function edit(){
		$tag = input('tag');
		if($tag==1){
			$data['cwebname']=input('cwebname');
			$data['cwebtongji']=input('cwebtongji');
			$data['cwebbottom']=input('cwebbottom');
			$data['vendor_url']=input('vendor_url');
			$data['return_url']=input('return_url');
			$data['answer_ad']=input('answer_ad');
			$data['city']=input('city');
			$res=Db::name('sys_config')->where('id',1)->update($data);
			if($res){
				cache::rm('config');
				exec("rm -rf /var/www/html/runtime/cache/");
				$this->success('修改成功',url('man/config/index'));
			}else{
				$this->error("修改失败");
			}

		}elseif($tag==2){
			$data['ctype']=input('ctype');
			$data['spend']=input('spend');
			$data['cappId']=input('cappId');
			$data['cappSecret']=input('cappSecret');
			$data['cappdwz']=input('cappdwz');
			$data['cappkey']=input('cappkey');
			$res=Db::name('sys_config')->where('id',1)->update($data);
			if($res){
				$config = Db::name('sys_config')->where('id',1)->find();
				cache::rm('config');
				exec("rm -rf /var/www/html/runtime/cache/");
				$this->success('修改成功',url('man/config/index'));
			}else{
				$this->error("修改失败");
			}

		}elseif($tag==3){

		}
	}

	public function video(){
		if(Request::instance()->ispost()){
			$data = Request::instance()->post();
			foreach($data as $k=>$val){
				Db::name('config')->where('key',$k)->update(['val'=>$val]);
			}
			$options = [
            'type'  =>  $this->GetCache(), 
            'expire'=>  60,
            'prefix'=>  'think',
            'host'  =>  '127.0.0.1',
            'port'  => '6379'
        ];
        Cache::connect($options);
		cache::rm('osman_config');
		}
		//if(!$config = Cache::store($this->GetCache())->get('osman_config')){
            $config = Db::name('config')->where('display',1)->column('key,val');
          // Cache::store($this->GetCache())->set('osman_config',$config,60);
        //}
        $this->assign('config',$config);
		return $this->fetch('video');
	}
	private function GetCache(){
        $cache = 'redis';
        if($_SERVER['HTTP_HOST']=='osman.cn'){
            $cache = 'file';
        }
        return $cache;
    }


    public function stopAutoJump(){
    	$domain_list_loading = Db::name('reach')->where('status',2)->column('domain');
    	if(!empty($domain_list_loading)){
	    	Db::name('config')->where('key','dump_whether')->update(['val'=>0]);
	    	$config = Db::name('config')->where('display',1)->column('key,val');
	        Cache::store($this->GetCache())->set('osman_config',$config,600);
	    	if(count($domain_list_loading)>0&&$domain_list_loading!=false){
	            foreach($domain_list_loading as $val){
	                Db::name('reach')->where('domain',$val)->update(['status'=>1]);
	                Db::name('gotos')->where('domain',$val)->delete();
	                Db::name('reach')->where('denp_domain',$val)->update(['denp_domain'=>'']);
	            }
	        }
    		return '操作成功';
	    }else{
	    	return '没有停止的操作';
	    }
    }
	
}